package com.testing.class18;

import com.testing.common.ExcelReader;
import com.testing.inter.InterKeyword;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 * @Classname LoginAPI
 * @Description 类型说明
 * @Date 2022/7/7 20:07
 * @Created by 特斯汀Roy
 */
public class LoginAPI extends InterKeyword {
    public String loginUrl;
    public String authUrl;
    public String logoutUrl="http://www.testingedu.com.cn:8081/inter/HTTP/logout";


    @BeforeClass
    @Parameters({"postUrl","authUrl"})
    public void setUp(String post,String auth){
        this.loginUrl =post;
        this.authUrl=auth;
    }

    @Test(priority = 0)
    public void testLoginRight(){
        postUrl(authUrl,"");
        saveJsonParam("token","$.token");
        addHeader("{\"token\":\"{token}\"}");
        postUrl(loginUrl,"username=roy&password=123456");
        Assert.assertTrue(assertJsonContains("$.msg","成功"));
        postUrl(logoutUrl,"");
    }

    @Test(priority = 2,dataProvider = "loginExcelData")
    public void testLoginParam(String username,String pwd,String result){
        postUrl(authUrl,"");
        saveJsonParam("token","$.token");
        addHeader("{\"token\":\"{token}\"}");
        postUrl(loginUrl,"username="+username+"&password="+pwd);
        Assert.assertTrue(assertJsonContains("$.msg",result));
        postUrl(logoutUrl,"");
    }


    @Test(priority = 1)
    public void loginNoAuth(){
        notUseHeader();
        postUrl(loginUrl,"username=roy&password=123456");
        Assert.assertTrue(assertJsonContains("$.msg","非法请求"));
    }

    @DataProvider
    public Object[][] loginData(){
        return new Object[][]{
                {"","123456","参数错误"},
                {"roy","","参数错误"},
                {"1","123456","参数错误"},
                {"royroyroyroyroyroyryo","123456","参数错误"},
                {"roy","123412341234123412341234","参数错误"}
        };
    }

    @DataProvider
    public Object[][] loginExcelData(){
        ExcelReader reader=new ExcelReader("Cases\\接口纯数据.xlsx");
        return reader.readAsMatrix();
    }


}
